//      Engine.cpp
//      
//      Copyright 2011 Philipp Müller <xi-vr@xi-intersection.net>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.

#include "Engine.hpp"
#include "EngineInput.hpp"
#include "EngineVideo.hpp"

#include <iostream>

#include <SDL.h>
#include <SDL_opengl.h>

/**********************************************************************
 * Öffentliche Methoden                                               *
 **********************************************************************/

/**
 * Konstruktor der Klasse "Engine"
 * 
 * @param &int    argc
 * @param &char** argv
 */

Engine::Engine(int &argc, char** &argv) {
	this->loadDefaultConfiguration();
	this->loadConfiguration();
	this->modifyConfiguration(argc, argv);
	
	this->init_sdl();
	
	this->video = new EngineVideo(this->config);
}

/**
 * Destruktor der Klasse "Engine"
 */

Engine::~Engine(void) {
	delete this->video;
	
	this->deinit_sdl();
}

/**
 * Hauptschleife
 */

void Engine::mainloop(void) {
	
}

/**********************************************************************
 * Geschützte Methoden                                                *
 **********************************************************************/

// KONFIGURATION =======================================================

void Engine::loadDefaultConfiguration(void) {
	this->config.video.resX = 640;
	this->config.video.resY = 480;
	
	this->config.video.depth = 32;
	
	this->config.video.fullscreen = 0;
}

void Engine::loadConfiguration(void) {
	
}

void Engine::modifyConfiguration(int&, char**&) {
	
}

// INITIALISIERUNG =====================================================

void Engine::init_sdl(void) {
	// SDL initialisieren:
	if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		std::cerr << "SDL initialization failed:\n " << SDL_GetError() << "\n";
		abort();
	}
	
	// Joysticks könnten ggf. von Interesse sein
	if (SDL_NumJoysticks() > 0) {
		for (int i = 0; i < SDL_NumJoysticks(); i++) {
			SDL_JoystickOpen(i);
		}
	}
	
	// Tastatureingabewiederholung
	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
}

// DEINITIALISIERUNG ===================================================

/**
 * 
 */

void Engine::deinit_sdl(void) {
	SDL_Quit();
}
